package com.lzh.edu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzh.commonutils.CommonResult;
import com.lzh.edu.entity.Course;
import com.lzh.edu.entity.Teacher;
import com.lzh.edu.query.TeacherQuery;
import com.lzh.edu.service.CourseService;
import com.lzh.edu.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author lizuhu
 * @since 2021-04-20
 */
@RestController
@CrossOrigin
@RequestMapping("/edu/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CourseService courseService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public CommonResult list() {
        List<Teacher> list = teacherService.list(null);
        return CommonResult.ok().data("items",list);
    }


    @ApiOperation(value = "根据id删除讲师")
    @DeleteMapping("/delete")
    public CommonResult deleteById(
            @ApiParam(name = "id",value = "讲师id",required = true)
            @RequestParam String id) {
        teacherService.removeById(id);
        return CommonResult.ok();
    }

    /**
     *
     * @param page 当前页
     * @param rows 一页的记录数
     * @return
     */
    @ApiOperation(value = "分页查询讲师列表")
    @GetMapping("/pageList")
    public CommonResult pageList(@RequestParam Long page, @RequestParam Long rows, TeacherQuery teacherQuery) {
        Page<Teacher> page1 = new Page<>(page, rows);
        teacherService.pageQuery(page1,teacherQuery);
        //每一页的总数据
        List<Teacher> records = page1.getRecords();
        //表的总记录数
        long total = page1.getTotal();
        return CommonResult.ok().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("/create")
    public CommonResult create(@RequestBody Teacher teacher) {
        System.out.println(teacher);
        boolean save = teacherService.save(teacher);
        return save ? CommonResult.ok():CommonResult.error();
    }

    @ApiOperation(value = "根据id查询讲师")
    @GetMapping("/findById")
    public CommonResult getById(@RequestParam String id) {
        Teacher teacher = teacherService.getById(id);
        return CommonResult.ok().data("item",teacher);
    }

    @ApiOperation(value = "根据id修改讲师")
    @PutMapping("/update")
    public CommonResult updateById(@RequestParam String id,@RequestBody Teacher teacher) {
        teacher.setId(id);
        teacherService.updateById(teacher);
        return CommonResult.ok();
    }

    @GetMapping("/frontPageList")
    public CommonResult frontPageList(@RequestParam Long page, @RequestParam Long rows) {
        Page<Teacher> teacherPage = new Page<>(page,rows);
        return CommonResult.ok();
    }

    @ApiOperation(value = "前端获取讲师信息")
    @GetMapping("/frontGetTeacher")
    public CommonResult getTeacherById(@RequestParam String id) {
        Teacher teacher = teacherService.getById(id);
        //根据讲师id获取课程信息
        List<Course> courseList = courseService.selectListByTeacherId(id);
        return CommonResult.ok().data("teacher",teacher).data("courseList",courseList);
    }

}

